package com.mvc4g.example.mockup.client.action;

import java.util.List;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.mvc4g.client.ActionInterface;
import com.mvc4g.client.Controller;
import com.mvc4g.example.mockup.client.Constants;
import com.mvc4g.example.mockup.client.MockUpService;
import com.mvc4g.example.mockup.client.MockUpServiceAsync;
import com.mvc4g.example.mockup.client.Session;
import com.mvc4g.example.mockup.client.bean.EmailBean;
import com.mvc4g.example.mockup.client.view.templates.EmailTemplate;
import com.mvc4g.example.mockup.client.view.templates.RootTemplate;
import com.mvc4g.example.mockup.client.view.views.EmailListView;

public class EmailListAction implements ActionInterface, Constants {

	private static MockUpServiceAsync services = MockUpService.Util
			.getInstance();

	@Override
	public void execute(final Controller controller, Object form) {
		services.getEmails((String) form, new AsyncCallback<List<EmailBean>>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onSuccess(List<EmailBean> result) {
				
				EmailTemplate emailView = (EmailTemplate) controller.getView(EMAIL_TEMPLATE);
				
				RootTemplate rootView = (RootTemplate) controller.getView(ROOT_TEMPLATE);
				rootView.changePage(emailView, null);
				
				EmailListView emailListView = (EmailListView) controller
						.getView(EMAIL_LIST_VIEW);

				Session.setAttribute(EMAIL_LIST, result);
				emailView.changePage(emailListView, GET_EMAILS);
			}

		});

	}

}
